<template>
  <view class="container">
   <view class="goods-list">
     <view  v-for="(item,index) in goodsList" :key="index" @click="gotoGoodsDetail(item)">
       <my-goods :item="item"></my-goods>
     </view>
   
   </view>
  </view>
</template>

<script>
  import { getGoodList } from '@/api/goodlist.js'
  export default {
    data() {
      return {
        queryObj:{
          query:'',
          cid:'',
          pagenum:1,
          pagesize:10
        },
        goodsList:[],
        isLoading:false,
        total:0
      };
    },
    onLoad(options) {
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      this.getGoodsList()
    },
    methods:{
     async getGoodsList(cb) {
       this.isLoading = true
        const { data } = await getGoodList(this.queryObj)
        if(data.meta.status !== 200) return uni.$showMsg()
          // 当前触底加载更多
        this.goodsList = [...this.goodsList,... data.message.goods]
        this.total = data.message.total
        this.isLoading = false
        // console.log(data);
        cb && cb()
      },
      // 触底加载更多事件 (固定用法)
      onReachBottom() {
        if(this.isLoading) return
        if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕')
        this.queryObj.pagenum += 1
        this.getGoodsList()
      },
      // 下拉刷新事件
      onPullDownRefresh() {
        // 重置成第一页数据
      this.queryObj.pagenum = 1
      this.total = 0
      this.isLoading = false
      this.goodsList = []
        this.getGoodsList(() => uni.stopPullDownRefresh())
      },
      // 跳转详情页
      gotoGoodsDetail(item) {
        uni.navigateTo({
          url:'../goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      }

    }
  }
</script>

<style scoped lang="scss">
.container {
  background-color: #fff;
  .goods-list {
   
  }
}
</style>
